﻿using System;
using System.Net;
using System.ComponentModel;
using System.Collections.Generic;
using System.Text;
using System.Management;

namespace FastAccess
{
    class Helper
    {
        public static bool AccessByProxy(ProxyDB.ProxyLsRow row,string Url)
        {
            Boolean Rs = false;
            WebClient wc = new WebClient();
            wc.Proxy = GetProxy(row);
            try
            {
                wc.DownloadString(Url);
                Rs = true;
            }
            catch (Exception ex)
            {
                Rs = false;
            }
            return Rs;
        }
        public static WebProxy GetProxy(ProxyDB.ProxyLsRow row)
        {
            WebProxy wp = new WebProxy(row.IP, int.Parse(row.Port));
            if (!row.UserName.Equals("Anymouse"))
            {
                NetworkCredential nc = new NetworkCredential(row.UserName, row.Password);
                wp.Credentials = nc;
            }
            return wp;
        }
        public static string GetKey()
        {
            string macAdress = string.Empty;
            ManagementClass mng = new ManagementClass("Win32_NetworkAdapterConfiguration");
            ManagementObjectCollection ins = mng.GetInstances();
            foreach (ManagementObject mo in ins)
            {
                if ((bool)mo["IPEnabled"] == true)
                //Response.Write("MAC address\t{0}"+mo["MacAddress"].ToString());
                {
                    macAdress = mo["MacAddress"].ToString().Replace(":", "");
                    mo.Dispose();
                    break;
                }
            }
            return macAdress.Substring(4, 8);
        }
    }
}
